-- Flower Bushes
local flower_bushes_table = { -- {name, desc, tiles}
    {'black', 'Black Flower Bush', 'black'},
    {'dark_grey', 'Dark Grey Flower Bush', 'dark_grey'},
    {'grey', 'Grey Flower Bush', 'grey'},
    {'white', 'White Flower Bush', 'white'},
    {'brown', 'Brown Flower Bush', 'brown'},
    {'pink', 'Pink Flower Bush', 'pink'},
    {'red', 'Red Flower Bush', 'red'},
    {'orange', 'Orange Flower Bush', 'orange'},
    {'yellow', 'Yellow Flower Bush', 'yellow'},
    {'green', 'Green Flower Bush', 'green'},
    {'dark_green', 'Dark Green Flower Bush', 'dark_green'},
    {'cyan', 'Cyan Flower Bush', 'cyan'},
    {'blue', 'Blue Flower Bush', 'blue'},
    {'violet', 'Violet Flower Bush', 'violet'},
    {'magenta', 'Magenta Flower Bush', 'magenta'}
}

-- Saplings
local saplings_table = { -- {name, desc, tiles, invimg, wieldimg
    {'black', 'Black Flower Bush Sapling', 'black', 'black', 'black'},
    {'dark_grey', 'Dark Grey Flower Bush Sapling', 'dark_grey', 'dark_grey', 'dark_grey'},
    {'grey', 'Grey Flower Bush Sapling', 'grey', 'grey', 'grey'},
    {'white', 'White Flower Bush Sapling', 'white', 'white', 'white'},
    {'brown', 'Brown Flower Bush Sapling', 'brown', 'brown', 'brown'},
    {'pink', 'Pink Flower Bush Sapling', 'pink', 'pink', 'pink'},
    {'red', 'Red Flower Bush Sapling', 'red', 'red', 'red'},
    {'orange', 'Orange Flower Bush Sapling', 'orange', 'orange', 'orange'},
    {'yellow', 'Yellow Flower Bush Sapling', 'yellow', 'yellow', 'yellow'},
    {'green', 'Green Flower Bush Sapling', 'green', 'green', 'green'},
    {'dark_green', 'Dark Green Flower Bush Sapling', 'dark_green', 'dark_green', 'dark_green'},
    {'cyan', 'Cyan Flower Bush Sapling', 'cyan', 'cyan', 'cyan'},
    {'blue', 'Blue Flower Bush Sapling', 'blue', 'blue', 'blue'},
    {'violet', 'Violet Flower Bush Sapling', 'violet', 'violet', 'violet'},
    {'magenta', 'Magenta Flower Bush Sapling', 'magenta', 'magenta', 'magenta'}
}

-- Bush Leaves
minetest.register_node('flower_bushes:bush_leaves', {
    description = ('Flower Bush Leaves'),
    drawtype = "allfaces_optional",
    waving = 1,
    tiles = {'flower_bushes_bush_leaves.png'},
    paramtype = "light",
    walkable = true,
    groups = {snappy = 3, flammable = 2, leaves = 1, hedge = 1},
    drop = {max_items = 1, items = {{items = {'flower_bushes:bush_leaves'}}}},
    on_timer = function(pos, ...)
        if not default.can_grow(pos) then
            -- try again 5 minutes later
            minetest.get_node_timer(pos):start(300)
            return
        end

        local meta = minetest.get_meta(pos)
        local color = meta:get("color") or saplings_table[math.random(#saplings_table)][1]
        local bush_name = 'flower_bushes:' .. color .. '_flower_bush'
        minetest.set_node(pos, {name = bush_name})
    end
})

for _, tab in ipairs(saplings_table) do
    local name = tab[1]
    local desc = tab[2]
    local tiles = tab[3]
    local invimg = tab[4]
    local wieldimg = tab[5]

    minetest.register_node('flower_bushes:' .. name .. '_flower_bush', {
        description = desc,
        drawtype = 'allfaces_optional',
        waving = 1,
        tiles = {'flower_bushes_bush_leaves.png^(flower_bushes_shadows.png^flower_bushes_' .. tiles .. '_flowers.png)'},
        paramtype = 'light',
        walkable = true,
        groups = {snappy = 3, flammable = 2, leaves = 1, dig_immediate = 3, hedge = 1, not_in_creative_inventory = 0},
        drop = {
            max_items = 1,
            items = {
                {items = {'flower_bushes:' .. name .. '_bush_sapling'}, rarity = 5},
                {items = {'flower_bushes:' .. name .. '_flowers'}}
            }
        },
        sounds = default.node_sound_leaves_defaults(),
        node_dig_prediction = 'flower_bushes:bush_leaves',

        after_dig_node = function(pos, oldnode, oldmetadata, digger)
            minetest.set_node(pos, {name = 'flower_bushes:bush_leaves'})
            minetest.get_node_timer(pos):start(math.random(500, 2000))
            local meta = minetest.get_meta(pos)
            meta:set_string("color", name)
        end
    })

    minetest.register_node('flower_bushes:' .. name .. '_bush_sapling', {
        description = desc,
        drawtype = 'plantlike',
        waving = 1,
        tiles = {'flower_bushes_' .. name .. '_sapling.png'},
        inventory_image = 'flower_bushes_' .. invimg .. '_sapling.png',
        wield_image = 'flower_bushes_' .. wieldimg .. '_sapling.png',
        groups = {snappy = 2, dig_immediate = 3, flammable = 2, attached_node = 1, sapling = 1},
        sunlight_propagates = true,
        paramtype = 'light',
        walkable = false,
        on_timer = function(pos, ...)
            if not default.can_grow(pos) then
                -- try again 5 minutes later
                minetest.get_node_timer(pos):start(300)
                return
            end
            minetest.set_node(pos, {name = 'flower_bushes:' .. name .. '_flower_bush'})
        end,
        selection_box = {type = 'fixed', fixed = {-0.25, -0.5, -0.25, 0.25, 0.875, 0.25}},
        sounds = default.node_sound_leaves_defaults(),
        on_construct = function(pos) minetest.get_node_timer(pos):start(math.random(500, 2000)) end
    })

    minetest.register_craft({
        output = 'flower_bushes:' .. name .. '_bush_sapling 1',
        recipe = {{'default:bush_sapling', 'dye:' .. name}}
    })

    minetest.register_craftitem('flower_bushes:' .. name .. '_flowers', {
        description = (desc .. ' Flowers'),
        inventory_image = 'flower_bushes_' .. invimg .. '_flowers.png',
        groups = {flower = 1, snappy = 3, attached_node = 1, flammable = 1, ['color_' .. name] = 1}
    })
end

